package com.ygo.feihua;

import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UploadFileListener;
import com.cc.chioceFileListView.BitmapOptimization;
import com.ygo.feihua.BmobTable.Kadian;
import com.ygo.feihua.BmobTable.MyUser;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.ygo.feihua.Management.*;
import com.ygo.feihua.util.*;
import com.yuyh.library.imgsel.config.*;
import android.graphics.*;
import com.yuyh.library.imgsel.*;
import com.yuyh.library.imgsel.ui.*;

public class KadianRenzheng extends AppCompatActivity implements OnClickListener
{
	EditText rz_name,rz_user,rz_phone,rz_dz,rz_jie;
	ImageView rz_im1,rz_im2,rz_im3,rz_im4;
	BmobFile im1,im2,im3,im4,im5;
	List<BmobFile> jt;
	TextView rz_jd1,rz_jd2,rz_jd3,rz_jd4,rz_jg;
	int a1,a2,a3,a4;
	private ProgressDialog pd1;
	Button rz_rz,rz_cs;
	Double jd=0.0;
	Double wd=0.0;
	String cs="";
	Gongju gj;
	private DialogUtils du;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		setContentView(R.layout.kadian_renzheng);
		gj=Gongju.getdx(KadianRenzheng.this);
		du=DialogUtils.getdx(this);
		
		rz_name = (EditText) findViewById(R.id.rz_name);
		rz_user = (EditText) findViewById(R.id.rz_user);
		rz_phone = (EditText) findViewById(R.id.rz_phone);
		rz_cs = (Button) findViewById(R.id.rz_cs);
		rz_dz = (EditText) findViewById(R.id.rz_dz);
		rz_jie = (EditText) findViewById(R.id.rz_jie);

		rz_im1 = (ImageView) findViewById(R.id.rz_im1);
		rz_im2 = (ImageView) findViewById(R.id.rz_im2);
		rz_im3 = (ImageView) findViewById(R.id.rz_im3);
		rz_im4 = (ImageView) findViewById(R.id.rz_im4);

		rz_jd1 = (TextView) findViewById(R.id.rz_jd1);
		rz_jd2 = (TextView) findViewById(R.id.rz_jd2);
		rz_jd3 = (TextView) findViewById(R.id.rz_jd3);
		rz_jd4 = (TextView) findViewById(R.id.rz_jd4);
		rz_jg = (TextView) findViewById(R.id.rz_jg);

		rz_rz = (Button) findViewById(R.id.rz_rz);
		gj.cshToolbar((Toolbar) findViewById(R.id.toolbar),"卡店认证");

		rz_im1.setOnClickListener(this);
		rz_im2.setOnClickListener(this);
		rz_im3.setOnClickListener(this);
		rz_im4.setOnClickListener(this);
		rz_rz.setOnClickListener(this);
		rz_jg.setOnClickListener(this);
		rz_cs.setOnClickListener(this);
	}
	Intent inte = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
	
	@Override
	public void onClick(View p1)
	{
		switch (p1.getId())
		{
			case R.id.rz_cs:
				Intent intent =new Intent(KadianRenzheng.this, KadianDitu.class);
				intent.putExtra("dk", 1);
				startActivityForResult(intent, 0);
				break;
			case R.id.rz_jg:
                View[] v=du.dialogt1(false,"","所认证的卡店可以是组织的聚集地。。地址必须写详细");
                Button b1=(Button) v[0];
                b1.setOnClickListener(new OnClickListener(){

                        @Override
                        public void onClick(View p1)
                        {
                            du.dis();
                            // TODO: Implement this method
                        }
                    });
				break;

			case R.id.rz_im1:		
				
				
				// 跳转到图片选择器
				ISNav.getInstance().toListActivity(this, Gongju.getPicConfig(1), 1);
				
				
				break;
			case R.id.rz_im2:
			//	startActivityForResult(new Intent(KadianRenzheng.this,NewWjxz.class).putExtra("lx",1).putExtra("path",Gongju.PATH_GEN),2);
				//startActivityForResult(inte,2);
				// 跳转到图片选择器
				ISNav.getInstance().toListActivity(this, Gongju.getPicConfig(2), 1);
				
				
				//startActivityForResult(new Intent(KadianRenzheng.this, NewTpxz.class), 2);
				break;
			case R.id.rz_im3:
				
				//startActivityForResult(new Intent(KadianRenzheng.this,NewWjxz.class).putExtra("lx",1).putExtra("path",Gongju.PATH_GEN),3);
				//startActivityForResult(inte,3 );
				// 跳转到图片选择器
				ISNav.getInstance().toListActivity(this, Gongju.getPicConfig(3), 1);
				
				
				//startActivityForResult(new Intent(KadianRenzheng.this, NewTpxz.class), 3);
				break;
			case R.id.rz_im4:
				
				//startActivityForResult(new Intent(KadianRenzheng.this,NewWjxz.class).putExtra("lx",1).putExtra("path",Gongju.PATH_GEN),4);
				//startActivityForResult(inte,4);
				// 跳转到图片选择器
				ISNav.getInstance().toListActivity(this, Gongju.getPicConfig(4), 1);
				
				//startActivityForResult(new Intent(KadianRenzheng.this, NewTpxz.class), 4);
				break;
			case R.id.rz_rz:
				String name,user,phone,dz,jie;
				name = rz_name.getText().toString().trim();
				user = rz_user.getText().toString().trim();
				phone = rz_phone.getText().toString().trim();
				dz = rz_dz.getText().toString().trim();
				jie = rz_jie.getText().toString().trim();

				UserManagement um=UserManagement.getDx();
					if (um == null)
				{
					gj.show("还没登录喵~");
					um.logInUser(this);
				//	startActivity(new Intent(this, YonghuZhuce.class));
				}
				else
				{

					if (name.equals("") || user.equals("") || phone.equals("") || cs.equals("") || dz.equals("") || jie.equals("") || jd == 0.0 || wd == 0.0)
					{
						gj.show("信息未填完整");
					}
					else
					{
						if (name.length() > 10)
						{
							gj.show("卡店名不能大于十个字");
						}
						else
						{
							if (a1 == 0 & a2 == 0 & a3 == 0 & a4 == 0)
							{
								gj.show("至少上传一张店铺图");
							}
							else
							{
								if (a1 == 1 | a2 == 1 | a3 == 1 | a4 == 1)
								{
									gj.show("请等待图片上传完成后再请求认证卡店");
								}
								else
								{

									pd1 = new ProgressDialog(KadianRenzheng.this);
									// 设置对话框显示的内容
									pd1.setMessage("上传中,请稍等");
									// 设置对话框的进度条风格
									pd1.setProgressStyle(ProgressDialog.STYLE_SPINNER);
									// 设置对话框的进度条是否显示进度
									pd1.setIndeterminate(true);
									pd1.show(); 

									List<BmobFile> lb=new ArrayList<BmobFile>();
									if (im1 != null )
									{
										lb.add(im1);
									}
									if (im2 != null )
									{
										lb.add(im2);
									}
									if (im3 != null )
									{
										lb.add(im3);
									}
									if (im4 != null )
									{
										lb.add(im4);
									}

									Kadian kd=new Kadian();

									kd.setKd_name(name);
									kd.setKd_user(user);
									kd.setKd_phone(phone);
									kd.setKd_cs(cs);
									kd.setKd_dz(dz);
									kd.setKdjt(lb);
									kd.setKdsh(2);
									kd.setKd_jie(jie);
									kd.setKduser(um.getUser());
									kd.setKd_jd(jd);
									kd.setKd_wd(wd);
									kd.save( new SaveListener<String>(){

											@Override
											public void done(String p1, BmobException p2)
											{
												if(p2==null){
													pd1.dismiss();
													gj.show("请求认证成功");
													finish();
												}else{
													gj.show("请求认证失败" + p2);
													
												}
												// TODO: Implement this method
											}
										});						
								}
							}					
						}}			
				}			

				break;		
		}
		// TODO: Implement this method
	}

	public String uridz(Uri uri){
	
		String []imgs={MediaStore.Images.Media.DATA};//将图片URI转换成存储路径  
		Cursor cursor=this.managedQuery(uri, imgs, null, null, null);  
		int index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);  
		cursor.moveToFirst();  
		String img_url=cursor.getString(index);  
		
		return img_url;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		// TODO: Implement this method
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode)
		{
			case 0:

				jd = data.getExtras().getDouble("jd");
				wd = data.getExtras().getDouble("wd");
				cs = data.getStringExtra("cs");
				rz_dz.setText(data.getStringExtra("xd"));
				break;
			case 1:
				if (resultCode == RESULT_OK ) {
					if (data != null) {
						//ArrayList<String> photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
						List<String> photos = data.getStringArrayListExtra(ISListActivity.INTENT_RESULT);
						final String tpp = photos.get(0);//data.getStringExtra("path");
				if (tpp.equals(""))
				{
				}
				else
				{
					a1 = 1;
					rz_jd1.setText("上传中");
					
					final BmobFile bmobFile = new BmobFile(new File(tpp));
					bmobFile.uploadblock( new UploadFileListener() {

							@Override
							public void done(BmobException p1)
							{
								if(p1==null){
									rz_im1.setImageBitmap(BitmapOptimization.getBitmap(tpp, rz_im1.getWidth()));
									im1=bmobFile;
									rz_jd1.setText("上传成功");
									a1=3;
								}else{
									rz_jd1.setText("上传失败" + p1);
									a1 = 0;
								}
								// TODO: Implement this method
							}
							@Override
							public void onProgress(Integer value) {
								// 返回的上传进度（百分比）
								rz_jd1.setText("上传进度"+value);

							}

						});
					
				}}}
				break;

			case 2:
				if (resultCode == RESULT_OK ) {
					if (data != null) {
						ArrayList<String> photos = data.getStringArrayListExtra(ISListActivity.INTENT_RESULT);

						final String tpp2 = photos.get(0);//data.getStringExtra("path");
						if (tpp2.equals(""))
				{
				}
				else
				{
					a2 = 1;
					rz_jd2.setText("上传中");
					final BmobFile bmobFile2 = new BmobFile(new File(tpp2));
					bmobFile2.uploadblock(new UploadFileListener() {

							@Override
							public void done(BmobException p1)
							{
								if(p1==null){
									rz_im2.setImageBitmap(BitmapOptimization.getBitmap(tpp2, rz_im1.getWidth()));
									im2=bmobFile2;
									rz_jd2.setText("上传成功");
									a2=3;
								}else{
									rz_jd2.setText("上传失败" + p1);
									a2 = 0;
								}
								// TODO: Implement this method
							}				
							@Override
							public void onProgress(Integer value) {
								// 返回的上传进度（百分比）
								rz_jd2.setText("上传进度"+value);
							}
						});
					
				}
				}}
				break;

			case 3:
				if (resultCode == RESULT_OK ) {
					if (data != null) {
						ArrayList<String> photos = data.getStringArrayListExtra(ISListActivity.INTENT_RESULT);

						final String tpp3 = photos.get(0);//data.getStringExtra("path");
						//final String tpp3 = data.getStringExtra("path");
				if (tpp3.equals(""))
				{
				}
				else
				{
					a3 = 1;
					rz_jd3.setText("上传中");
					final BmobFile bmobFile3 = new BmobFile(new File(tpp3));
					bmobFile3.uploadblock(new UploadFileListener() {

							@Override
							public void done(BmobException p1)
							{
								if(p1==null){
									rz_im3.setImageBitmap(BitmapOptimization.getBitmap(tpp3, rz_im1.getWidth()));
									im3=bmobFile3;
									rz_jd3.setText("上传成功");
									a3=3;
								}else{
									rz_jd3.setText("上传失败" + p1);
									a3 = 0;
								}
								// TODO: Implement this method
							}				
							@Override
							public void onProgress(Integer value) {
								// 返回的上传进度（百分比）
								rz_jd3.setText("上传进度"+value);
							}
							
						});
				}
				}}
				
				break;
			case 4:
				
				if (resultCode == RESULT_OK ) {
					if (data != null) {
						ArrayList<String> photos = data.getStringArrayListExtra(ISListActivity.INTENT_RESULT);

						final String tpp4 = photos.get(0);//data.getStringExtra("path");
						//final String tpp4 = data.getStringExtra("path");
				if (tpp4.equals(""))
				{
				}
				else
				{
					a4 = 1;
					rz_jd4.setText("上传中");
					final BmobFile bmobFile4 = new BmobFile(new File(tpp4));
					bmobFile4.uploadblock(new UploadFileListener() {

							@Override
							public void done(BmobException p1)
							{
								if(p1==null){
									rz_im4.setImageBitmap(BitmapOptimization.getBitmap(tpp4, rz_im1.getWidth()));
									im4=bmobFile4;
									rz_jd4.setText("上传成功");
									a4=3;
								}else{
									rz_jd4.setText("上传失败" + p1);
									a4= 0;
								}
								// TODO: Implement this method
							}				
							@Override
							public void onProgress(Integer value) {
								// 返回的上传进度（百分比）
								rz_jd4.setText("上传进度"+value);
							}
						});
					
				}}}
				break;
		}

	}
}
